#include "search_atalog.h"

/*
int main()
{
	if (LogLine_Init(&w_logline) == 0)
	{}
	while (1)
	{
		//读取日志文件
		read_file();
		sleep(10);
		//判断日志文件日期
		//在日志文件中检测指定内容
		//判断内容
	}
	return 0;
}
*/
//日志时间初始化，当时间字符串已经赋值，则初始化月天时分秒，当没有赋值则返回-1
int LogLine_Init(struct LogLine *logline)
{
	if (strlen(logline->StrLine) > STRTIME)
	{
		memcpy(logline->logtime.StrTime,logline->StrLine,STRTIME);
	}
	if (strlen(logline->logtime.StrTime) < STRTIME)
	{
		return -1;
	}
	logline->logtime.Mon=(char*)malloc(3);
	logline->logtime.Day=(char*)malloc(2);
	logline->logtime.Time=(char*)malloc(8);
	memcpy(logline->logtime.Mon, logline->logtime.StrTime, 3);
	memcpy(logline->logtime.Day, logline->logtime.StrTime+4, 2);
	memcpy(logline->logtime.Time, logline->logtime.StrTime+7, 8);
	return 0;
}
void LogLine_free(struct LogLine *logline)
{
	free(logline->logtime.Mon);
	free(logline->logtime.Day);
	free(logline->logtime.Time);
}
//读取日志文件
int read_file()
{
	char filename[]="/var/log/messages";
	FILE *fp1;
	char StrLine[MAXLINE];
	//日志时间记录
	struct LogLine logline;

	if ((fp1 = fopen(filename,"r")) == NULL)
	{
		printf("error\n");
		fclose(fp1);
		return -1;
	}
	while(!feof(fp1))
	{
		fgets(StrLine,1024,fp1);
		strcpy(logline.StrLine,StrLine);
		memcpy(logline.logtime.StrTime,StrLine,STRTIME);
		LogLine_Init(&logline);
		//判断是否上次记录
		if (strlen(w_logline.logtime.StrTime) == STRTIME)
		{
			//判断时间先后
			if (LogTime_cmp(&logline.logtime, &w_logline.logtime) == 0)
			{
				continue;
			}
		}
		//每行搜索关键字
		if (LogKey_search(StrLine) == 1)
		{
			FILE *fp;
			fp=fopen("/var/log/ata_messgaes","w");
			fprintf(fp, "%s", StrLine);
			//printf("%s",StrLine);
			fclose(fp);
		}
	}
	if (LogLine_cpy(&logline, &w_logline) == 0)
	{
		//printf("%s\n",w_logline.logtime.Day);
		//printf("%s\n",w_logline.logtime.StrTime);
		//LogTime_Init(&w_logline);
	}
	fclose(fp1);
	return 1;
}
//整个字符串的复制
int LogLine_cpy(struct LogLine* logline_a, struct LogLine* logline_b)
{
	int i = 0;
	while (logline_a->StrLine[i] != '\0')
	{
		logline_b->StrLine[i] = logline_a->StrLine[i];
		i++;
	}
	logline_b->StrLine[i] = '\0';
	LogLine_Init(logline_b);
	return 0;
}
//时间比较，返回1，大于当前时间，返回0，小于当前时间
int LogTime_cmp(struct LogTime* logtime_a, struct LogTime* logtime_b)
{
	if (LogMon_cmp(logtime_a->Mon,logtime_b->Mon) == 1)
	{
		return 1;
	}
	else if (LogMon_cmp(logtime_a->Mon,logtime_b->Mon) == 0)
	{
		//compare day
		if (atoi(logtime_a->Day) > atoi(logtime_b->Day))
			return 1;
		else if(atoi(logtime_a->Day) == atoi(logtime_b->Day))
		{
			//compare time
			if(LogHMS_cmp(logtime_a->Time,logtime_b->Time) == 1)
				return 1;
			else if (LogHMS_cmp(logtime_a->Time,logtime_b->Time) == 0)
				return 0;
		}
	}
	return 0;
}
//mon_a当前时间，mon_b原来时间，返回0，继续比天，返回1则停止比较
int LogMon_cmp(char* mon_a, char* mon_b)
{
	if (strcmp(mon_a,mon_b) == 0)
		return 0;
	if (Mon_num(mon_a) != 1 && Mon_num(mon_a) == (Mon_num(mon_b)-1))
		return 1;
	else if(Mon_num(mon_a) == 1 && Mon_num(mon_b) == 12)
		return 1;
	return 0;
}
//将月份转换为数字
int Mon_num(char* mon_a)
{
	if (strcmp(mon_a,"Jan") == 0)
		return 1;
	if (strcmp(mon_a,"Feb") == 0)
		return 2;
	if (strcmp(mon_a,"Mar") == 0)
		return 3;
	if (strcmp(mon_a,"Apr") == 0)
		return 4;
	if (strcmp(mon_a,"May") == 0)
		return 5;
	if (strcmp(mon_a,"Jun") == 0)
		return 6;
	if (strcmp(mon_a,"Jul") == 0)
		return 7;
	if (strcmp(mon_a,"Aug") == 0)
		return 8;
	if (strcmp(mon_a,"Sep") == 0)
		return 9;
	if (strcmp(mon_a,"Oct") == 0)
		return 10;
	if (strcmp(mon_a,"Nov") == 0)
		return 11;
	if (strcmp(mon_a,"Dec") == 0)
		return 12;
	return 0;
}
//比较时分秒，返回1好，返回0继续判断
int LogHMS_cmp(char* mon_a, char* mon_b)
{
	int temp_i = -1;
	char *temp_a_h, *temp_a_m, *temp_a_s;
	char *temp_b_h, *temp_b_m, *temp_b_s;
	temp_a_h = (char*)malloc(2);
	temp_a_m = (char*)malloc(2);
	temp_a_s = (char*)malloc(2);
	temp_b_h = (char*)malloc(2);
	temp_b_m = (char*)malloc(2);
	temp_b_s = (char*)malloc(2);
	memcpy(temp_a_h, mon_a, 2);
	memcpy(temp_a_m, mon_a+2, 2);
	memcpy(temp_a_s, mon_a+4, 2);
	memcpy(temp_b_h, mon_b, 2);
	memcpy(temp_b_m, mon_b+2, 2);
	memcpy(temp_b_s, mon_b+4, 2);
	if (atoi(temp_a_h) == atoi(temp_b_h))
	{
		if (atoi(temp_a_m) == atoi(temp_b_m))
		{
			if (atoi(temp_a_s) > atoi(temp_b_s))
				temp_i = 1;
			else
				temp_i = 0;
		}
		else if (atoi(temp_a_m) > atoi(temp_b_m))
		{
			temp_i = 1;
		}
		else
			temp_i = 0;
	}
	else if (atoi(temp_a_h) > atoi(temp_b_h))
		temp_i = 1;
	else
		temp_i = 0;
	free(temp_a_h);
	free(temp_a_m);
	free(temp_a_s);
	free(temp_b_h);
	free(temp_b_m);
	free(temp_b_s);
	return temp_i;
}
//记录该条记录是否有关键字，返回0不全部包含关键字，返回1全部包含
int LogKey_search(char *search)
{
	int temp_j=0;
	char *key_words_1[10]={"ata","Emask","SAct","SErr","action","frozen"};
	char *key_words_2[10]={"ata","cmd "};
	char *key_words_3[10]={"res ","Emask"};
	char *key_words_4[10]={"ata","status"};
	char *key_words_5[10]={"ata","SError"};
	char *key_words_6[10]={"ata","error"};
	char *key_words_7[10]={"ata","errno"};
	//char *key_words[10]={"Cleanup","Temporary","Directories"};
	//char *key_words_1[10]={"Cleanup","Temporary","Directories"};
        //char *key_words_2[10]={"dbus-daemon","nm-dispatcher"};
        //char *key_words_3[10]={"NetworkManager","info","address","server identifier 192.168.14.254"};
	temp_j = search_key(search, key_words_1) | search_key(search, key_words_2) | search_key(search, key_words_3) \
		| search_key(search, key_words_4) | search_key(search, key_words_5) | search_key(search, key_words_6) \
		| search_key(search, key_words_7) ;
	return temp_j;
}
//字符串是否包含某几个关键字
int search_key(char *search, char* key_words[])
{
	int temp_i = 0;
	while(key_words[temp_i] != NULL)
	{
		if (strstr(search,key_words[temp_i]) != NULL)
		{
			temp_i++;
		}
		else
		{
			return 0;
		}
	}
	return 1;
}
